package com.dwd.basic.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/16 10:02
 * @Version 1.0
 *
 * 发送复杂邮件工具类
 */
@Component
public class SendMail {
    @Autowired
    private  JavaMailSender mailSender;


    /**
     * 发送邮件方法
     *
     * @param multipart          是否开启多文件
     * @param subject            主题
     * @param text               文本
     * @param html               是否开启html文本
     * @param attachmentFilename 文件名
     * @param file               文件路径
     * @param setTo              发送给谁
     * @param setfrom            从哪个用户发送，配置yaml的信息，用户名密码
     * @throws MessagingException
     * @Author wendi
     */
    public  void sendMail(Boolean multipart, String subject, String text, Boolean html, String attachmentFilename, File file, String setTo, String setfrom) throws MessagingException {
        System.out.println(mailSender);
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multipart);
        helper.setSubject(subject);
        //开启html标签
        helper.setText(text, html);
        //附件
        helper.addAttachment(attachmentFilename, file);
        helper.setTo(setTo);
        helper.setFrom(setfrom);
        mailSender.send(mimeMessage);
    }


    /**
     * 发送邮件方法
     *
     * @param multipart 是否开启多文件
     * @param subject   主题
     * @param text      文本
     * @param html      是否开启html文本
     * @param setTo     发送给谁
     * @param setfrom   从哪个用户发送，配置yaml的信息，用户名密码
     * @throws MessagingException
     * @Author wendi
     */
    public void sendMail(Boolean multipart, String subject, String text, Boolean html, String setTo, String setfrom) throws MessagingException {
        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multipart);
        helper.setSubject(subject);
        //开启html标签
        helper.setText(text, html);
        helper.setTo(setTo);
        helper.setFrom(setfrom);
        mailSender.send(mimeMessage);
    }
}
